import store from '@/store'
import router from '@/router'

import NProgress from 'nprogress' // 引入一份进度条插件
import 'nprogress/nprogress.css'

// 定义白名单
const WhiteList = ['/login', '/404']

// 前置导卫
router.beforeEach(async(to, from, next) => {
  NProgress.start()

  const token = store.getters.token
  if (token) {
    if (to.path === '/login') {
      next({ path: '/' })
      NProgress.done()
    } else {
      const userInfo = store.state.user.setUser
      if (Object.keys(userInfo).length <= 0) {
        const res = await store.dispatch('user/peraonLsData')
        console.log(res)
      }
      next()
    }
  } else {
    if (WhiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach((to, from) => {
  NProgress.done()
})
